package com.qh.book.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qh.book.VO.ImgslistVO;
import com.qh.book.VO.ResultVO;
import com.qh.book.converter.BookImgs2ImgDTO;
import com.qh.book.dataobj.BookImgs;
import com.qh.book.dto.ImgDTO;
import com.qh.book.enums.ResultEnum;
import com.qh.book.exception.BsException;
import com.qh.book.service.BookImgsService;
import com.qh.book.utils.ResultVOUtil;
import com.qh.book.utils.StrUtils;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/imgs")
public class BookImgsApi {
	
	@Autowired
	private BookImgsService bookImgsService;

	@GetMapping("/bookinfoimgs")
	public ResultVO<Object> bookimgs(@RequestParam(value = "id", defaultValue = "") String id) {
	
		//对传递的参数进行检查
		//String bookid = id.trim();
		ResultVO<Object> resultVO = new ResultVO<Object>();
		ImgslistVO imgslistVO = new ImgslistVO();
		List<BookImgs> imgList = new ArrayList<BookImgs>();
		
		
		if("".equals(id)||!StrUtils.isInteger(id)) {
			resultVO = ResultVOUtil.returnError(ResultEnum.PARM_FAIL);
			return resultVO;
		}
		try {
			imgList = bookImgsService.findByBookId(id);
		}catch(BsException e) {
			resultVO = ResultVOUtil.returnError(ResultEnum.IMGS_ISEMPTY);
			return resultVO;
		}
		List<ImgDTO> imgDTOList = BookImgs2ImgDTO.converter(imgList);
		imgslistVO.setBookId(id);
		imgslistVO.setList(imgDTOList);
		imgslistVO.setTotal(imgDTOList.size());
		//封装返回数据
		resultVO = ResultVOUtil.returnSuccess(ResultEnum.SCUUESS, imgslistVO);
		
		
		return resultVO;
	}
}
